package zhongbo.java.air.monitor.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.common.vo.ResultVo;
import zhongbo.java.air.monitor.model.AirAlertLog;
import zhongbo.java.air.monitor.service.IAirAlertLogService;

@RestController
@RequestMapping("/alertdialog")
@Api(tags = "告警操作")
public class AirAlertLogController {
    @Autowired
    private IAirAlertLogService airAlertLogService;

    @PostMapping("/add")
    @ApiOperation("新增告警")
    public ResultVo<AirAlertLog> add(@RequestBody AirAlertLog airAlertLog){
        if (airAlertLog == null || airAlertLog.equals("")){
            return ResultVo.error("数据为空");
        }
        int i = airAlertLogService.addAirAlertLog(airAlertLog);
        if (i <= 0){
            return ResultVo.wrong(404,"添加失败!");
        }
        return ResultVo.ok(airAlertLog);
    }

    @GetMapping("/delete")
    @ApiOperation("删除告警")
    public ResultVo<AirAlertLog> delete(String id){
        if (id == null || id.equals("")){
            return ResultVo.error("数据为空");
        }
        AirAlertLog airAlertLog = airAlertLogService.selectById(id);
        if (airAlertLog.equals("") || airAlertLog == null){
            return ResultVo.error("无该信息");
        }
        airAlertLogService.deleteAirAlertLog(id);
        return ResultVo.success();
    }

    @PostMapping("/update")
    @ApiOperation("更新信息")
    public ResultVo<AirAlertLog> update(@RequestBody AirAlertLog airAlertLog){
        if (airAlertLog == null || airAlertLog.equals("")){
            return ResultVo.error("数据为空");
        }
        AirAlertLog log = airAlertLogService.selectById(airAlertLog.getId());
        if (log == null || log.equals("")){
            return ResultVo.error("无该数据");
        }
        int i = airAlertLogService.updateAirAlertLog(airAlertLog);
        if (i<=0){
            return ResultVo.error("更新失败");
        }
        return ResultVo.success();
    }

    @GetMapping("/datil")
    @ApiOperation("告警详细信息")
    public ResultVo<AirAlertLog> selectById(String id){
        if (id == null ||id.equals("")){
            return ResultVo.error("id为空");
        }
        AirAlertLog airAlertLog = airAlertLogService.selectById(id);
        if (airAlertLog == null || airAlertLog.equals("")){
            return ResultVo.error("查询失败");
        }
        return ResultVo.ok(airAlertLog);
    }

    @GetMapping("/page")
    @ApiOperation("status分页查询")
    public ResultVo<PageVo<AirAlertLog>> page(String status,Integer pageNumber,Integer pageSize){
        PageVo pageVo = airAlertLogService.page(status, pageNumber, pageSize);
        return ResultVo.ok(pageVo);
    }
}
